<%@page import="vn.edu.ptithcm.pim.constant.Constants"%>
<script type="text/javascript">
var AddTasksData = {
    attachedFiles : []	
}

function proccessDeleteTask(taskId){
	jQuery.ajax({
		type : "post",
		data : "type=<%=Constants.DELETE_TASK%>" + "&taskId=" + taskId,
		url : "<%=request.getContextPath()%>/ServletServer",
		success : function(data) {
			if (data == '<%=Constants.OK_MESSAGE%>') {
				window.location.reload();
			}
		}
	});
}

function pagingSubmit(){
	var type = jQuery('select[name=type]').val();	
	var currentPageNum = jQuery("#paging").find(".page-selector select").val();
	var pageSize = jQuery("#paging").find("#itemsPerPage select").val();			
	window.location.href = "${bean.pagingUrl}&type="+ type + "&currentPageNum=" + currentPageNum + "&pageSize=" + pageSize;			
}

function changeTypeOfTasks(){
	var type = jQuery('select[name=type]').val();	
	window.location.href = "${bean.pagingUrl}&type="+ type;
}

function startUpload(id) {  	
	var cmd = jQuery('input[name=cmd]').val();
	var numberOfFiles = jQuery("#fileUploadsQueue").children().size();
	if(numberOfFiles == 0){
		if(cmd == 'addTasks'){
			submitForm('addTasks');	
		}else if(cmd == 'updateTask'){
			submitForm('updateTask');
		}		
	}else{
	   jQuery('#' + id).uploadifyUpload();	
	}
}

jQuery(document).ready(function(){
	   jQuery('select[name=type]').change(function(){
		   changeTypeOfTasks();
	   });
		
	   jQuery("input[name='starttime']").datepicker(pickerOpts);
	   jQuery("input[name='endtime']").datepicker(pickerOpts);
	   
	   if (jQuery("#summary").val() == 'null') {
			jQuery("#summary").val('');
	   }
	   if (jQuery("#description").val() == 'null') {
			jQuery("#description").val('');
	   }
	   if (jQuery("#additionalinformation").val() == 'null') {
		    jQuery("#additionalinformation").val('');
	   }
	   
	   jQuery('.v-img-action').click(function() {
			var taskId = $(this).attr('id');
			jQuery('#dialogDeleteTask').html('<spring:message code="message.confirm.delete.task"/>');
			jQuery('#dialogDeleteTask').dialog({
				modal : true,
				resizable : false,
				height : 120,
				width : 370,
				title : '<spring:message code="message"/>',
				buttons : {
					'<spring:message code="button.ok"/>' : function() {
						proccessDeleteTask(taskId);
					},
					'<spring:message code="button.cancel"/>' : function() {
						jQuery('#dialogDeleteTask').dialog("close");
					}
				}
			});
		});
	   
	   jQuery('#filter').click(function(){		     
			 jQuery('input[name=cmd]').val('filter');
 		     submitForm('myTasks'); 
		});
	   
	   jQuery('#reset').click(function(){
		   	 jQuery('input[name=cmd]').val('reset');
		     submitForm('myTasks'); 
		});
	   
	   jQuery('#save').click(function(){
		   	 jQuery('input[name=cmd]').val('save');
		     submitForm('myTasks'); 
	   });	 
	   
	   var requiredMajorVersion = 10;
	    var requiredMinorVersion = 0;
	    var requiredRevision = 0;
	    var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	    if (!hasReqestedVersion) {
	      jQuery('#nosupportFlash').css("display", "block");
	      jQuery('#supportFlash').css("display", "none");
	      return;
	    }

	    jQuery("#fileUploads").uploadify({
	       'uploader': "<%=request.getContextPath()%>/swf/uploadify.swf",
	       'cancelImg': '<%=request.getContextPath()%>/images/cancel.png',
	       'script' : '<%=request.getContextPath()%>/ServletServer?type=<%=Constants.UPLOAD_FILES%>',  
	       'folder': '/UserData/Uploads',
	       'multi': true,
	       'sizeLimit': 10485760,
	       'queueSizeLimit': 10,
	       'removeCompleted': false,          
	       'displayData': 'percentage',
	       'expressInstall' : '<%=request.getContextPath()%>/swf/expressInstall.swf',
	       'fileExt': '*.jpg;*.jpeg;*.png',
	       'fileDesc': '*.jpg;*.jpeg;*.png',
	       
	       'onComplete': function (event, ID, fileObj, response, data) {        	 
	     	  AddTasksData.attachedFiles.push(response);
	       },
	       'onAllComplete': function (evt, data) {              
	           jQuery('input[name=_attachedfiles]').val(AddTasksData.attachedFiles.join(';'));
	           var cmd = jQuery('input[name=cmd]').val();
	           if(cmd == 'addTasks'){
	   			submitForm('addTasks');	
	   		  }else if(cmd == 'updateTask'){
	   			submitForm('updateTask');
	   		  }
	       },
	       'onError': function (event, ID, fileObj, errorObj) {
	       }
	    });		
});
</script>